package com.project.hniemall.auth.controller;

import com.project.common.constant.AuthServerConstant;
import com.project.common.utils.R;
import com.project.common.vo.MemberRespVo;
import com.project.hniemall.auth.feign.MemberFeignService;
import com.project.hniemall.auth.vo.UserLoginVo;
import com.project.hniemall.auth.vo.UserRegistVo;
import io.prometheus.client.Collector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;


/**
 * @author sky
 * @date 2023-05-17
 */
@Controller
public class LoginController {

    @Autowired
    private MemberFeignService memberFeignService;

    @GetMapping("/index")
    public String loginPage() {
        return "index.html";
    }

    @GetMapping("/reg")
    public String regPage() {
        return "reg.html";
    }

    @GetMapping("/sms/sendcode")
    public R sencode(){
        return R.ok();
    }

    @PostMapping("/regist")
    public String regist(@Valid UserRegistVo vo, BindingResult result, RedirectAttributes redirectAttributes){
        if (result.hasErrors()){
            return "reg.html";
        }

        memberFeignService.regist(vo);
        return "index.html";
    }

    @PostMapping("/login")
    //前端传来k,v参数不需要加@RequestBody
    public String login(UserLoginVo vo, RedirectAttributes redirectAttributes, HttpSession session) {
        System.out.println("123");
        //远程登录
        R r = memberFeignService.login(vo);
        if (r.getCode() == 0) {
            return "redirect:http://localhost:10002/";
        } else {
            return "index.html";
        }
    }
}
